﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Modules.ACTP.Components;

namespace ASPNETVN.PORTAL.Modules.ACTP.Controls
{
    public partial class AssignCertificationToEmployee : AddEditItemControl
    {
        #region Extra Properties
        private int _EmployeeID;
        public int EmployeeID
        {
            get
            {
                if (_EmployeeID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["EmployeeID"] != null)
                        _EmployeeID = Int32.Parse(Request.Params["EmployeeID"]);
                }
                return _EmployeeID;
            }
            set { _EmployeeID = value; }
        }

        private int _CertificationID;
        public int CertificationID
        {
            get
            {
                if (_CertificationID == 0)
                {
                    if (HttpContext.Current != null && Request.Params["CertificationID"] != null)
                        _CertificationID = Int32.Parse(Request.Params["CertificationID"]);
                }
                return _CertificationID;
            }
            set { _CertificationID = value; }
        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            this.ButtonCancel = btnCancel;
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                // Employee Detail.
                EmployeeDetailControl1.EmplopyeeID = this.EmployeeID;

                this.MyDataBinding();
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            EmployeeCertification employeecertification = new EmployeeCertification();
            employeecertification.ModuleID = this.ModuleID;
            employeecertification.EmployeeID = this.EmployeeID;
            employeecertification.CertificationID = Convert.ToInt32(ddlCertification.SelectedValue);
            if (string.IsNullOrEmpty(txtCertifiedDate.Text) == false)
            {
                employeecertification.CertifiedDate = Convert.ToDateTime(txtCertifiedDate.Text);
            }

            if (string.IsNullOrEmpty(txtExpiredDate.Text) == false)
            {
                employeecertification.ExpiredDate = Convert.ToDateTime(txtExpiredDate.Text);
            }

            employeecertification.CreatedBy = employeecertification.UpdatedBy = PortalSecurity.CurrentUser.Identity.Email;
            employeecertification.CreatedDate = employeecertification.UpdatedDate = DateTime.Now;
            this.ItemID = employeecertification.Insert();

            //Refresh.
            this.MyDataBinding();
        }

        private void MyDataBinding()
        {
            ddlCertification.Items.Clear();
            ddlCertification.Items.Add(new ListItem("[Choose a certification]", "0"));
            IList<Certification> exists = Certification.SelectCollectionBy_EmployeeID(this.ModuleID, this.EmployeeID);
            gvList.DataSource = exists;
            gvList.DataBind();

            // Available Certifications.
            IList<Certification> alls = Certification.SelectCollectionAll(this.ModuleID);
            Start:
            foreach (Certification all in alls)
            {
                foreach (Certification exist in exists)
                {
                    if (all.CertificationID == exist.CertificationID)
                    {
                        alls.Remove(all);
                        goto Start;
                    }
                }
            }
            ddlCertification.DataSource = alls;
            ddlCertification.DataBind();
        }

        protected void gvList_RowCommand(object sender, System.Web.UI.WebControls.GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                int certification = Convert.ToInt32(e.CommandArgument);
                EmployeeCertification.DeleteEmployeeCertification(certification, this.EmployeeID);

                this.MyDataBinding();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.EmployeeID, ModulePages.EmployeeDetail);
            Response.Redirect(url);
        }
    }
}
